package com.androider.timememory.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.androider.timememory.R;
import com.androider.timememory.utils.Dict;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ContactsAdapter extends BaseAdapter {
	private ArrayList<HashMap<String, Object>> mData;
	private LayoutInflater mInflater;
	public HashMap<Integer, Boolean> isSelected; 
	
	public ContactsAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
		mInflater = LayoutInflater.from(context);
		mData = data;
		init();
	}
	
	private void init() {
		isSelected = new HashMap<Integer, Boolean>();
		for (int i = 0; i < mData.size(); i++) {    
            isSelected.put(i, false);    
		}
	}
	
	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int position) {
		return mData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if(null == convertView) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.add_people_item, null);
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.number = (TextView) convertView.findViewById(R.id.phone);
			holder.cbx = (CheckBox) convertView.findViewById(R.id.check_box);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.name.setText((String) mData.get(position).get(Dict.C_name));
		holder.number.setText((String) mData.get(position).get(Dict.C_number));
		holder.cbx.setChecked(isSelected.get(position));
		
		return convertView;
	}
	
	public final class ViewHolder {
		public TextView name;
		public TextView number;
		public CheckBox cbx;
	}
}
